---
title: "conjoint AMCE"
author: "Dai Yamao"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
rm(list=ls())

require(cjoint)
require(tidyverse)

dat_cjoint <- read_csv("data/conjoint_data_tr.csv")
dat_cjoint[,c(18:22)] <- data.frame(lapply(dat_cjoint[,c(18:22)],as.factor))

dat_cjoint <- dat_cjoint %>% 
  mutate(Sect = recode(Sect,
                      "1" = "sunni",
                      "2" = "shia",
                      "3" = "christian",
                      "4" = "kurd",
                      "5" = "others"))

dat_cjoint <- dat_cjoint %>% 
  mutate(sunni = ifelse(Sect == 1, 1, 0),
         shia = ifelse(Sect == 2, 1, 0),
         kurd = ifelse(Sect == 4, 1, 0),
         christian = ifelse(Sect == 3, 1, 0),
         minority = ifelse(Sect == 5, 1, 0))

#dat_cjoint$Q3_1[dat_cjoint$Q3_1 == 6] <- NA
#dat_cjoint$Q3_2[dat_cjoint$Q3_2 == 6] <- NA
#dat_cjoint$Q3_3[dat_cjoint$Q3_3 == 6] <- NA
#dat_cjoint$Q3_7[dat_cjoint$Q3_7 == 6] <- NA

dat_cjoint <- dat_cjoint %>% 
  mutate(trust_president = 7 - Q3_1,
         trust_pm = 7 - Q3_2,
         trust_parliament = 7 - Q3_3,
         trust_party = 7 - Q3_7)
dat_cjoint <- dat_cjoint %>% 
  mutate(trust_gov = (trust_president + trust_pm + trust_parliament + trust_party) /4, na.rm = TRUE)

dat_cjoint <- dat_cjoint %>% 
  mutate(Trust_gov = case_when(trust_gov < 2 ~ "low",
                               trust_gov >= 2 & trust_gov < 5 ~ "middle",
                               trust_gov >= 5 ~ "high"))

dat_cjoint <- dat_cjoint %>% 
  mutate(Trust_govnt = case_when(trust_gov < 3.5 ~ "low",
                               trust_gov >= 3.5 ~ "high"))

dat_cjoint <- dat_cjoint %>% 
  mutate(gotovote = recode(Q2,
                      "1" = "will vote",
                      "2" = "will not vote"))
dat_cjoint$Sect <- as.factor(dat_cjoint$Sect)
dat_cjoint$gotovote <- as.factor(dat_cjoint$gotovote)
dat_cjoint$Trust_gov <- as.factor(dat_cjoint$Trust_gov)
dat_cjoint$Trust_govnt <- as.factor(dat_cjoint$Trust_govnt)
dat_cjoint$trust_parliament <- as.factor(dat_cjoint$trust_parliament)

```

# conjoint model: whole
```{r}
str(dat_cjoint)

attr_list <- list()             
attr_list[["place"]] <- c("Capital", "Provincial capital", "Other")
attr_list[["sect"]] <- c("Sunni", "Shia", "Kurd", "Minority", "Mixture")
attr_list[["reason"]] <- c("Unemployment", "Wealfare", "Public goods", "Inflation", 
                           "Environment", "Corruption", "Political system", 
                           "Election fraus", "Intervention")
attr_list[["timing"]] <- c("Non-election", "During-campaign", "Election day", "Post-election") 
attr_list[["organizer"]] <- c("Sadr", "Imtidad", "Non-party", "Tribal chief", "Non-organizer")

cjoint_design <- makeDesign(type = "constraints",
                            attribute.levels = attr_list)
cjoint_pool <- amce(choice ~ place + sect + reason + timing + organizer,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(cjoint_pool)
summary(cjoint_pool)
```

# subset: sect
```{r}
dat_sunni <- dat_cjoint %>% 
  filter(Sect == "sunni") 
dat_shia <- dat_cjoint %>% 
  filter(Sect == "shia") 
dat_kurd <- dat_cjoint %>% 
  filter(Sect == "kurd") 
dat_minority <- dat_cjoint %>% 
  filter(Sect == "christian" | Sect == "others") 

# sunni
cj_sunni <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_sunni,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_sunni)
summary(cj_sunni)

# shia
cj_shia <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_shia,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_shia)
summary(cj_shia)

# kurd
cj_kurd <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_kurd,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_kurd)
summary(cj_kurd)

# minority
cj_minority <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_minority,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_minority)
summary(cj_minority)

# subset
reg01 <- amce(choice ~ place + sect + reason + timing + organizer,
                    data = subset(dat_cjoint, Sect == "shia"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg01)
summary(reg01)
```

# sect: interaction
```{r, fig.height = 10, dpi = 300}
reg02 <- amce(choice ~ place*Sect + sect*Sect + reason*Sect + timing*Sect + organizer*Sect,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg02, facet.names = c("Sect"), show.all = TRUE)
summary(reg02)
```

# vote: interaction
```{r}
reg03 <- amce(choice ~ place*gotovote + sect*gotovote + reason*gotovote + timing*gotovote + organizer*gotovote,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg03, facet.names = c("gotovote"), show.all = TRUE)
summary(reg03)

dat_vote <- dat_cjoint %>% 
  filter(Q2 == 1)
dat_novote <- dat_cjoint %>% 
  filter(Q2 == 2)

cj_vote <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_vote,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_vote)
summary(cj_vote)

cj_novote <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_novote,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_novote)
summary(cj_novote)
```

# trust
```{r}
dat_trust <- dat_cjoint %>% 
  filter(trust_gov >= 2.75)
dat_distrust <- dat_cjoint %>% 
  filter(trust_gov <= 2)

cj_trust <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_trust,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_trust)
summary(cj_trust)

cj_distrust <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_distrust,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_distrust)
summary(cj_distrust)

reg04 <- amce(choice ~ place*Trust_gov + sect*Trust_gov + reason*Trust_gov + timing*Trust_gov + organizer*Trust_gov,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg04, facet.names = c("Trust_gov"), show.all = TRUE)
summary(reg04)

reg05 <- amce(choice ~ place*Trust_govnt + sect*Trust_govnt + reason*Trust_govnt + timing*Trust_govnt + organizer*Trust_govnt,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg05, facet.names = c("Trust_govnt"), show.all = TRUE)
summary(reg05)
```

# education
```{r}
dat_lowedu <- dat_cjoint %>% 
  filter(Education <= 4)
dat_highedu <- dat_cjoint %>% 
  filter(Education >= 5)

cj_lowedu <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_lowedu,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_lowedu)
summary(cj_lowedu)

cj_highedu <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_highedu,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_highedu)
summary(cj_highedu)
```

# income
```{r}
dat_low <- dat_cjoint %>% 
  filter(Income <= 3)
dat_high <- dat_cjoint %>% 
  filter(Income >= 4)

cj_low <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_low,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_low)
summary(cj_low)

cj_high <- amce(choice ~ place + sect + reason + timing + organizer,
             data = dat_high,
             respondent.id = "id",
             cluster = TRUE,
             design = cjoint_design,
             na.ignore = TRUE)
plot(cj_high)
summary(cj_high)
```
